define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'signup/signup/index' + location.search,
                    // add_url: 'signup/signup/add',
                    // edit_url: 'signup/signup/edit',
                    del_url: 'signup/signup/del',
                    multi_url: 'signup/signup/multi',
                    import_url: 'signup/signup/import',
                    table: 'signup',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        /*{field: 'id', title: __('Id')},*/
                        {field: 'number', title: __('Number')},
                        {field: 'user.nickname', title: __('打卡用户'), operate: 'LIKE'},
                        {field: 'address_has_one.tel', title: __('联系方式'), operate: 'LIKE'},
                        {field: 'activity.title', title: '活动标题', operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'activity.image', title: '活动图片', operate: 'LIKE', table: table, class: 'autocontent', events: Table.api.events.images, formatter: Table.api.formatter.images},
                        {field: 'nationwideRanking', title: '全国排名',
                            formatter: function (value, index) {
                                return '第'+ value + '名';
                            }
                        },
                        {field: 'groupRanking', title: '组别排名',
                            formatter: function (value, index) {
                                return '第'+ value + '名';
                            }
                        },
                        {field: 'myGroup', title: '打卡用户组别'},
                        {field: 'save_number', title: __('Save_number')},
                        {field: 'createtime', title: '最新打卡时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                // 导出图片过大处理
                exportOptions: {
                    ignoreColumn: [0, 'operate'],
                    onBeforeSaveToFile: function (data, fileName, type, charset, encoding, bom) {
                        return $.fn.bootstrapTable.defaults.extend.savestatus;
                    },
                    onCellHtmlHyperlink: function ($cell, rowIndex, colIndex, href, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;
                        var result = '';

                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            } else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            } else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "40").attr("height", "40").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                } else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    }
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
